'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
      const { ctx } = this;
      const res = await this.app.mysql.select('students');
      console.log(res);
      await ctx.render('home.html');
  }
    
    async cookiePage() {
        let number = 1;
        if (this.ctx.cookies.get('number')) {
            // cookie 存在
            number = this.ctx.cookies.get('number');
            number++;
            this.ctx.cookies.set('number', number);
        } else {
            // cookie 不存在
            this.ctx.cookies.set('number', number);
        }
        await this.ctx.render('cookie.html', {
            number: number
        })
    }

    async sessionPage() {
        let number = 1;
        if (this.ctx.session.number) {
            // session 存在
            number = this.ctx.session.number;
            number++;
            this.ctx.session.number = number;
        }else{
            this.ctx.session.number = number;
        }
        await this.ctx.render('/session.html', {
            number: number
        })
    }
}

module.exports = HomeController;
